
import requests
import json
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from Config import *

class OperUtil:

    # 点击元素
    def ClickEle(self, driver, selector):
        ele = driver.find_element(By.CSS_SELECTOR, selector)
        ele.click()
        time.sleep(Config["Spider"]["ost"])
        return ele
    
    # 双击元素
    def DbClickEle(self, driver, selector):
        ele = driver.find_element(By.CSS_SELECTOR, selector)
        actions = ActionChains(driver)
        actions.double_click(ele).perform()
        time.sleep(Config["Spider"]["ost"])
        return ele

    # 悬停事件
    def MovetoEle(self, driver, selector):
        ele = driver.find_element(By.CSS_SELECTOR, selector)
        actions = ActionChains(driver)
        actions.move_to_element(ele).perform()
        return ele

    # 录入文本框
    def EnterInput(self, driver, selector, content):
        ele = driver.find_element(By.CSS_SELECTOR, selector)
        ele.click()
        # ele.clear()
        ele.send_keys(Keys.CONTROL, "a")
        ele.send_keys(Keys.DELETE)
        ele.send_keys(content)
        time.sleep(Config["Spider"]["ost"])
        return ele
    
    def EnterInput1(self, driver, labelName, content):
        ele = driver.find_element(By.XPATH, '//label[contains(text(), "'+labelName+'")]').find_element(By.XPATH, './following-sibling::*')
        if ele.tag_name != 'input':
            ele = ele.find_element(By.CSS_SELECTOR, 'input')
        # .find_element(By.XPATH, './following-sibling::input[1]')
        ele.click()
        # ele.clear()
        ele.send_keys(Keys.CONTROL, "a")
        ele.send_keys(Keys.DELETE)
        ele.send_keys(content)
        time.sleep(Config["Spider"]["ost"])
        return ele
    
    def EnterInput2(self, ele, content):
        ele.click()
        # ele.clear()
        ele.send_keys(Keys.CONTROL, "a")
        ele.send_keys(Keys.DELETE)
        ele.send_keys(content)
        time.sleep(Config["Spider"]["ost"])
        return ele
    
    def EnterInputItem(self, driver, selector, content):
        ele = driver.find_element(By.CSS_SELECTOR, selector)
        ele.click()
        # ele.clear()
        ele.send_keys(Keys.CONTROL, "a")
        ele.send_keys(Keys.DELETE)
        for c in content:
            ele.send_keys(content)
            time.sleep(0.5)
        # time.sleep(Config["Spider"]["ost"])
        return ele
    
    
if __name__ == "__main__":
    print('oper...')

    

